/*
 * $Id: SystemRequestDUI.java,v 1.7 2010/04/01 14:27:37 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.ui.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.ui.pojo.UiRequest;
import com.ge.healthcare.autosc.ui.pojo.UiRequestSorter;

public class SystemRequestDUI extends BaseDaoUI {
	public static UserInterfaceDAO uiDAO;
	public static final String START_DATE = "startDate";
	public static final String DAYS_BACK = "daysBack";
	public static final String SYSTEM_ID = "id";

	@SuppressWarnings("unchecked")
	public static List<UiRequest> getRequests(Hashtable<?, ?> data) {
		List<UiRequest> requests;
		try {
			String startDate = data.get(START_DATE).toString();
			int daysBack = Integer.parseInt(data.get(DAYS_BACK).toString());
			String systemId = cleanupInputString(data.get(SYSTEM_ID).toString());
			List<UiRequest> autoscRequests = null;
			autoscRequests = uiDAO.getRequests(systemId, startDate, daysBack);

			if (ASCUtil.isNullOrEmptyList(autoscRequests)) {
				StringBuffer msg = new StringBuffer(
						"No File Records for Query: ");
				msg.append(SYSTEM_ID).append(": ").append(systemId).append(" ");
				msg.append(DAYS_BACK).append(": ").append(daysBack).append(" ");
				msg.append(START_DATE).append(": ").append(startDate);
				throw new RuntimeException(msg.toString());
			} else {
				requests = new ArrayList<UiRequest>();
				// Need to sort results
				UiRequestSorter sorter = new UiRequestSorter(autoscRequests);
				Map<Long, List> requestMap = sorter.getMap();
				List<Long> keys = new ArrayList<Long>(sorter.keySet());
				Collections.sort(keys);
				for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
					Long time = (Long) iterator.next();
					if (requestMap.containsKey(time)) {
						List<UiRequest> timeList = requestMap.get(time);
						if (!ASCUtil.isNullOrEmptyList(timeList)) {
							for (UiRequest timeRequest : timeList) {
								requests.add(timeRequest);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			requests = new ArrayList<UiRequest>();
			UiRequest request = new UiRequest();
			setErrorMessage(request, e);
			requests.add(request);
		}
		return requests;
	}

	public void setUiDAO(UserInterfaceDAO uiDAO) {
		SystemRequestDUI.uiDAO = uiDAO;
	}
}
